//
//  ResetPassViewController.m
//  specialine
//
//  Created by 往返网络 on 17/2/7.
//  Copyright © 2017年 com. All rights reserved.
//

#import "ResetPassViewController.h"
#import "ResetPassViewController_1.h"
#import "ResetPassView1.h" //view
#import "PublicMethod.h"
//定义获取验证码URL
#define URL_GETPASSWORDCODE [NSString stringWithFormat:@"%@/Appcustomernew/GetPassword/getCode?type=get",BASE_URL]
@interface ResetPassViewController ()<UITextFieldDelegate,ResetPassDelegate_1>
@property (nonatomic,strong)AFHTTPSessionManager * manger;
@end

@implementation ResetPassViewController

- (void)viewWillAppear:(BOOL)animated{

    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
    

}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self createUI];
    
    
    // Do any additional setup after loading the view.
}

- (void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBarHidden = NO;

    
}

- (void)createUI{

    
    ResetPassView1 * resetview1 = [[ResetPassView1 alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) andTitle:_titlestring];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    resetview1.resetpassdelegate1 = self;

    [self.view addSubview:resetview1];
    

}


#pragma mark -- --- - - - - - -ResetPassword1 Delegate  -- -- - - - -- - -
//传递输入的手机号，进入下一页
- (void)enterResetPassVC2BtnType:(ResetPassView1BtnType)resetPassType andPhoneStr:(NSString *)phonestring{

    switch (resetPassType) {
            
        case ResetPassView1BtnClickReturn:
            
            //返回按钮
            [self dismissViewControllerAnimated:YES completion:nil];

            break;
            
            case ResetPassView1BtnClickSendYanzhengCode:
            
            //验证手机号，发送请求，进入下一页
            
//            if (![PublicMethod isValidateTel:phonestring]) {
//                
//                ALERT(@"提示", @"手机号格式错误", nil, @"确定", nil);
//                
//                return;
//                
//            }
//            
            
            [self getCode:phonestring];
            
            
            break;
            
                      
            
    }







}

#pragma mark  - -- - - - -   获取验证码 - - - - - - - - - -- 
- (void)getCode:(NSString * )phonestr{

    
    //字符串反转之后，再发送出去 phonenum --- 反转 －－－＊2 －－－ 转化为a-z
        
        NSMutableString *s = [NSMutableString string];
        
        for (NSUInteger i = phonestr.length; i>0; i--) {
            
            [s appendString:[phonestr substringWithRange:NSMakeRange(i-1, 1)]];
            
        }
        
        NSInteger s1 = [s integerValue]*2;
        
        NSString * s2 = [NSString stringWithFormat:@"%ld",s1];
        
        NSLog(@"s2 %@",s2);
        
        NSArray * arr = @[@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j"];
        
        NSMutableString * endstring = [NSMutableString string];
        
        for (NSUInteger i = 0; i < s2.length; i++) {
            
            //截取1个字符串
            NSString * s3 =  [s2 substringWithRange:NSMakeRange(i, 1)];
            
            NSInteger loc = [s3 integerValue];
            
            // 转化为小写字母
            
            NSString * s4 = arr[loc];
            
            [endstring appendString:s4];
            
            
        }
    
         //开始动画
        UIImageView * im = [PublicMethod startAnimation];
    
        NSLog(@"endstring %@",endstring);
        
        NSString * url = [NSString stringWithFormat:@"%@&mobile=%@",URL_GETPASSWORDCODE,endstring];
        
        NSLog(@"get 验证码 url %@",url);
        
        [self.manger GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            
            if (responseObject) {
                
                NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                
                NSInteger code = [dict[@"code"]integerValue];
                
                if (code == 0) {
                    
                    //获取验证码成功
                    
                    [im stopAnimating];
                    
                    NSLog(@"get code ok");
                    
                    ALERT(@"提示", dict[@"info"], self, @"确定", nil);
                    
                    //成功之后进入下一页
                    ResetPassViewController_1 * resetvc2 = [[ResetPassViewController_1 alloc]init];
                    
                    //保存上级页面的手机号
                    resetvc2.phonestring = phonestr;
                    
                    [self.navigationController pushViewController:resetvc2 animated:YES];
                    

                    
                }else{
                    
                    [im stopAnimating];

                    NSLog(@"get code no");
                    
                    ALERT(@"提示", dict[@"info"], self, @"确定", nil);
                    
                }
                
                
            }
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
            [im stopAnimating];

            ALERT(@"提示", @"网络请求失败", self, @"确定", nil);
            
        }];
        
        
        
    }




- (AFHTTPSessionManager*)manger{

    _manger = [AFHTTPSessionManager manager];
    
    _manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    _manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];


    return _manger;
    


}



- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
